﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SilverNotes.Common;

namespace SilverNotes
{
  public partial class SelectedPages : UserControl
  {
    private readonly WriteableBitmap[] _selectedPages = new WriteableBitmap[8];
    private WriteableBitmap _blankPage;



    public static void SetAcceptDrop(UIElement element, bool acceptDrop)
    {
      element.SetValue(AcceptDropProperty, acceptDrop);
    }

    public static bool GetAcceptDrop(UIElement element)
    {
      return (bool) element.GetValue(AcceptDropProperty);
    }

    // Using a DependencyProperty as the backing store for AcceptDrop.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AcceptDropProperty =
        DependencyProperty.RegisterAttached("AcceptDrop", typeof(bool), typeof(SelectedPages), new PropertyMetadata(false));

    private MainPageViewModel ViewModel
    {
      get
      {
        return (MainPageViewModel) DataContext;
      }
    }

    /// <summary>
    /// Gets selected pages.
    /// </summary>
    public WriteableBitmap[] Pages
    {
      get 
      {
        return _selectedPages;
      }
    }

    public SelectedPages()
    {
      InitializeComponent();
      CreateBlankPage();

      for (int i = 0; i < _selectedPages.Length; i++)
      {
        _selectedPages[i] = _blankPage;
      }
    }

    private void CreateBlankPage()
    {
      _blankPage = new WriteableBitmap((int)Constants.PageWidth, (int)Constants.PageHeight);
      unchecked
      {
        for (int i = 0; i < _blankPage.PixelHeight; i++)
        {
          for (int j = 0; j < _blankPage.PixelWidth; j++)
          {
            _blankPage.Pixels[i + j*_blankPage.PixelWidth] =  (int) 0xffffffff; // White
          }
        }
      }
    }

    private void PageSelected(object sender, RoutedEventArgs e)
    {
      var wb = new WriteableBitmap(ViewModel.SelectedItem.RootVisual, null);
      _selectedPages[GetIdxByButton(sender)] = wb;
      ((ToggleButton) sender).Content = new Image {Source = wb};
    }

    private void PageDeselected(object sender, RoutedEventArgs e)
    {
      _selectedPages[GetIdxByButton(sender)] = _blankPage;
      ((ToggleButton)sender).Content =null;
    }

    private int GetIdxByButton(object btn)
    {
      if (btn == tb1) return 0;
      if (btn == tb2) return 1;
      if (btn == tb3) return 2;
      if (btn == tb4) return 3;
      if (btn == tb5) return 4;
      if (btn == tb6) return 5;
      if (btn == tb7) return 6;
      if (btn == tb8) return 7;
      throw new ArgumentOutOfRangeException("btn");
    }

    public void SetContent(UIElement target)
    {
      var toggleButton = (ToggleButton)target;
      toggleButton.IsChecked = false;
      toggleButton.IsChecked = true; // Reset state;
    }
  }
}
